local omega = require("omega")
package.path = ("%s;%s"):format(
    package.path,
    omega.storage_path.get_code_path("LuaLoader", "?.lua")
)
local coromega = require("coromega").from(omega)

coromega:create_websocket_server("0.0.0.0", 1042):when_new_conn(function(conn)
    conn:when_new_msg(function(msg)
        print("websocket server received (not intercepted): ", msg)
        conn:send_message("server echo: " .. msg)
    end)

    conn:send_message("server hello")
    local received = conn:receive_message()
    print("websocket server received (first message): ", received)
end):when_dead(function(deadReason)
    print("websocket server dead, reason: ", deadReason)
end)

coromega:start_new(function()
    coromega:sleep(1.0)
    local conn = coromega:connect_to_websocket("ws://127.0.0.1:1042")
    conn:when_new_msg(function(msg)
        print("websocket client received (not intercepted): ", msg)
    end)
    conn:send_message("client hello")
    local received = conn:receive_message()
    print("websocket client received (first message): ", received)
    conn:send_message("client hello 1")
    coromega:sleep(1.0)
    conn:send_message("client hello 2")
    coromega:sleep(1.0)
    conn:send_message("client hello 3")
    local received = conn:receive_message()
    print("websocket client received: ", received)
    coromega:sleep(1.0)
    conn:send_message("client hello 4")
    local received = conn:receive_message()
    print("websocket client received: ", received)
end)
coromega:run()
